using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace NavmeshPathfinding
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Core : Microsoft.Xna.Framework.Game
    {
        public static Random random = new Random();

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public Navmesh navmesh;
        public Interaction interaction;
        public Camera camera;
        public List<Agent> agentList;
        public Light light0;
        public Light light1;
        public Light light2;

        public bool moveRandomly = false;

        //light


        public Core()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferHeight = 768;
            graphics.PreferredBackBufferWidth = 1024;
            //graphics.ToggleFullScreen();

            navmesh = new Navmesh(this);
            interaction = new Interaction(this);
            camera = new Camera(this);
            agentList = new List<Agent>();
            for (int i = 0; i < Config.Agent.COUNT; i++)
                agentList.Add(new Agent(this));
            light0 = new Light(this);
            light1 = new Light(this);
            light2 = new Light(this);

            navmesh.DrawOrder = 2;
            foreach (Agent agent in agentList)
                agent.DrawOrder = 1;
            interaction.DrawOrder = 3;

            Components.Add(navmesh);
            Components.Add(interaction);
            Components.Add(camera);
            foreach (Agent agent in agentList)
                Components.Add(agent);
            Components.Add(light0);
            Components.Add(light1);
            Components.Add(light2);


            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            graphics.PreferMultiSampling = true;
            IsMouseVisible = true;

            //light
            light0.diffuseColor = new Vector3(0.8f, 0.8f, 0.8f);
            light0.direction = new Vector3(-1.0f, -1.0f, 1.0f);
            light0.specularColor = new Vector3(1.0f, 1.0f, 1.0f);

            light1.enabled = false;
            light2.enabled = false;

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            base.Draw(gameTime);
        }

        public void addAgent()
        {
            Agent agent = new Agent(this);
            agentList.Add(agent);
            agent.DrawOrder = 1;
            Components.Add(agent);
            navmesh.addAgent(agent);
            if (moveRandomly)
            {
                agent.moveRandomly();
                agent.move();
            }
        }

        public void removeAgent()
        {
            if (agentList.Count > 0)
            {
                Agent agent = agentList.Last();
                agentList.Remove(agent);
                Components.Remove(agent);
                navmesh.removeAgent(agent);
            }
        }

        public void toggleMoveAgentsRandomly()
        {
            moveRandomly = !moveRandomly;
            if (moveRandomly)
            {
                foreach (Agent agent in agentList)
                {
                    agent.moveRandomly();
                    agent.move();
                }
            }
            else
            {
                foreach (Agent agent in agentList)
                {
                    agent.stopMovingRandomly();
                    agent.stop();
                }
            }
        }
    }
}
